﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

//custom headers
#include "ListHeaderWidgets.h"
#include "PaintingUtils.h"

//Qt headers
#include <QHBoxLayout>
#include <QLabel>
#include <QMouseEvent>
#include <QPainter>
#include <QWidget>
#include "GbmpNew.h"
#include "TranslatorManager.h"
#include "DebugMessage.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    ListHeaderWidget::ListHeaderWidget(QWidget *parent) :QWidget(parent),
        m_pCheckBox(NEW_AS_QT (QLabel)),
        m_checkState(Qt::CheckState::Unchecked),
        m_bMousePressedOnCheckBox(false),
        m_isEditOperationVisible(true)
    {
        QHBoxLayout *pLayout = NEW_AS_QT (QHBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pLayout, L"pLayout为空", L"GMEP", L"2024-01-30");
        pLayout->setContentsMargins(0, 0, 0, 0);
        pLayout->setSpacing(0);

        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pCheckBox, L"m_pCheckBox为空", L"GMEP", L"2024-01-30");
        m_pCheckBox->setFixedSize(21, 24);
        m_pCheckBox->setStyleSheet("QLabel{background:transparent;}");

        QLabel *pLabel = NEW_AS_QT( QLabel,QString::fromStdWString(GBMP_TR(L"全部构件")));
        DBG_WARN_AND_RETURN_VOID_UNLESS(pLabel, L"pLabel为空", L"GMEP", L"2024-01-30");
        pLabel->setAlignment(Qt::AlignLeft|Qt::AlignVCenter);

        pLayout->addSpacing(10);
        pLayout->addWidget(m_pCheckBox);
        pLayout->addWidget(pLabel);
        pLayout->addStretch();

        setStyleSheet("QWidget{background:#D7DDE6;}");
        setLayout(pLayout);        
    }

    ListHeaderWidget::~ListHeaderWidget()
    {
    }

    void ListHeaderWidget::SetIsEditOperationVisible(bool isEditOperationVisible /*= true*/)
    {
        m_isEditOperationVisible = isEditOperationVisible;
    }

    void ListHeaderWidget::ChangeCheckState(Qt::CheckState state)
    {
        if (m_checkState != state)
        {
            m_checkState = state;
            update();
        }
    }

    void ListHeaderWidget::mousePressEvent(QMouseEvent * event)
    {
        if (event)
        {
            if (m_pCheckBox && m_pCheckBox->rect().contains(event->pos()))
            {
                m_bMousePressedOnCheckBox = true;
                return;
            }
        }
    }

    void ListHeaderWidget::mouseReleaseEvent(QMouseEvent * event)
    {
        if (event)
        {
            if (m_pCheckBox && m_pCheckBox->rect().contains(event->pos()))
            {
                if (m_bMousePressedOnCheckBox)
                {
                    if (m_checkState == Qt::CheckState::PartiallyChecked)
                    {
                        m_checkState = Qt::CheckState::Checked;
                    }
                    else if (m_checkState == Qt::CheckState::Checked)
                    {
                        m_checkState = Qt::CheckState::Unchecked;
                    }
                    else
                    {
                        m_checkState = Qt::CheckState::Checked;
                    }

                    update();
                    emit CheckStateChanged(m_checkState);

                    m_bMousePressedOnCheckBox = false;
                    return;
                }
            }
        }
        m_bMousePressedOnCheckBox = false;
    }
    void ListHeaderWidget::paintEvent(QPaintEvent * event)
    {
       

        QPainter painter(this);
        painter.setPen(Qt::NoPen);
        painter.setBrush(QBrush("#D7DDE6"));
        painter.drawRect(rect());

        // 若不允许编辑，则不需要checkbox
        if (!m_isEditOperationVisible)
            return;
        // draw checkbox
        QRect boxRect = QRect(rect().topLeft().rx() + 10, rect().topLeft().ry() + 6, 12, 12);
        if (m_checkState == Qt::CheckState::Checked)
        {
            PaintingUtils::DrawBorder(&painter, boxRect, QMargins(1, 1, 1, 1), QMargins(2, 2, 2, 2), QColor(82, 145, 221, 255), QColor(82, 145, 221, 255));

            QRect tickRect = boxRect.marginsAdded(QMargins(-2, -2, -2, -2));
            QImage tickImage = QImage(":/images/icon_tick_ffffff_12.png");
            painter.setRenderHint(QPainter::RenderHint::SmoothPixmapTransform);
            painter.drawPixmap(tickRect, QPixmap::fromImage(tickImage));
        }
        else if(m_checkState == Qt::CheckState::Unchecked)
        {
            PaintingUtils::DrawBorder(&painter, boxRect, QMargins(1, 1, 1, 1), QMargins(2, 2, 2, 2), QColor(189, 199, 213, 255));
        }
        else
        {
            PaintingUtils::DrawBorder(&painter, boxRect, QMargins(1, 1, 1, 1), QMargins(2, 2, 2, 2), QColor(82, 145, 221, 255));

            QRect squareRect = boxRect.marginsRemoved(QMargins(2, 2, 2, 2));
            painter.fillRect(squareRect, QBrush(QColor(82, 145, 221, 255), Qt::BrushStyle::SolidPattern));
        }
    }
}